//dom加载完成后执行的js
;$(function(){

    //全选的实现
    $(".check-all").click(function(){
        $(".ids").prop("checked", this.checked);
    });
    $(".ids").click(function(){
        var option = $(".ids");
        option.each(function(i){
            if(!this.checked){
                $(".check-all").prop("checked", false);
                return false;
            }else{
                $(".check-all").prop("checked", true);
            }
        });
    });
    //$('.selectpicker').selectpicker({
    //    style: 'btn-white',
    //    dropupAuto:false
    //});



    //时间筛选器

    var dateTimeInput = $("input.J_datetime");
    if (dateTimeInput.length) {
        Wind.css('datePicker');
        Wind.use('datePicker', function () {
            dateTimeInput.datePicker({
                time: true,
                hour:12
            });
        });
    }




    var dateInput = $("input.J_date");

    if (dateInput.length) {
        Wind.css('datePicker');
        Wind.use('datePicker', function () {
            dateInput.datePicker({
                time: false
            });
        });
    }

    $(document).on("click",".ajax-del",function(){

        var that=$(this);
        if(!confirm('你确定删除吗')){
            return false;
        }

        $.ajax({
            type: "GET",
            url: $(this).attr('href'),
            data: "att="+$(this).data('href'),
            success: function(data){


                if (data.status == '1') {
                    $.scojs_message(data.info, $.scojs_message.TYPE_OK);

                    that.parents("tr").remove();



                }else{

                    $.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
                }

            }

        });

        return false;

    });


    $(document).on("click",".ajax-switch",function(){
        var that=$(this);

        $.ajax({
            type: "GET",
            url: $(this).attr('href'),
            data: "att="+$(this).data('href'),
            success: function(data){


                if (data.status == '1') {
                    $.scojs_message(data.info, $.scojs_message.TYPE_OK);

                    var s =that.find("i");
                    var t=s.attr('class');

                    if(t=="icon-check-circle"){
                        s.attr("class","icon-times-circle");
                    }
                    if(t=="icon-times-circle"){
                        s.attr("class","icon-check-circle");
                    }



                }else{

                    $.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
                }

            }

        });

        return false;
    });



    /*//全选的实现
     $(".check-all").click(function(){
     $(".ids").prop("checked", this.checked);
     });
     $(".ids").click(function(){
     var option = $(".ids");
     option.each(function(i){
     if(!this.checked){
     $(".check-all").prop("checked", false);
     return false;
     }else{
     $(".check-all").prop("checked", true);
     }
     });
     });*/

    //提示
    $(document).on("click",".ajax-get",function(){
        var target;
        var that = this;
        if ( $(this).hasClass('confirm') ) {

            if($(this).data('title') !== undefined){
                if(!confirm( $(this).data('title'))){
                    return false;
                }
            }else{
                if(!confirm('你确定执行吗')){

                    return false;
                }
            }


        }


        if ( (target = $(this).attr('href')) || (target = $(this).attr('url')) ) {


            $(that).addClass('disabled').attr('autocomplete','off').prop('disabled',true);
            $.get(target).success(function(data){
                ajax_sumbit(that,data);
            });

        }
        return false;
    });

    $(document).on("click",".ajax-post",function(){

        var target,query,form;
        var target_form = $(this).attr('target-form');
        var that = this;


        if( ($(this).attr('type')=='submit') || (target = $(this).attr('url'))  ){
            form = $('.'+target_form);

            if (form.get(0)==undefined){
                return false;
            }else {



                if($(this).attr('url') !== undefined){
                    target = $(this).attr('url');
                }else{
                    target = form.get(0).action;
                }
                query = form.serialize();


                var btn = $(this),
                    form = btn.parents('form.J_ajaxForm');

                //批量操作 判断选项
                if (btn.data('subcheck')) {

                    if (!form.find('input.ids:checked').length) {
                        $.scojs_message("请至少选择一项", $.scojs_message.TYPE_ERROR);
                        return false;
                    }

                }

                // return false;
                if ( $(this).hasClass('confirm') ) {

                    if($(this).data('title') !== undefined){
                        if(!confirm( $(this).data('title'))){
                            return false;
                        }
                    }else{
                        if(!confirm('你确定执行吗')){

                            return false;
                        }
                    }


                }
                $(that).addClass('disabled').attr('autocomplete','off').prop('disabled',true);
                $.post(target,query).success(function(data){
                    ajax_sumbit(that,data);

                });




            }


        }
        return false;
    });









});


//标签页切换(无下一步)
function ajax_sumbit(that,data) {





    if (data.status == '1') {

        if (data.url) {
            $.scojs_message(data.info+ ' 页面即将自动跳转~', $.scojs_message.TYPE_OK);

        }else{
            $.scojs_message(data.info, $.scojs_message.TYPE_OK);
            // updateAlert(data.info ,'alert-success');
        }
        setTimeout(function(){
            if (data.url) {
                location.href=data.url;
            }else if( $(that).hasClass('no-refresh')){
                $(that).removeClass('disabled').prop('disabled',false);
            }else{
                location.reload();
            }
        },1500);
    }else{
        $.scojs_message(data.info, $.scojs_message.TYPE_ERROR);

        setTimeout(function(){
            if (data.url) {
                location.href=data.url;
            }else{
                $(that).removeClass('disabled').prop('disabled',false);
            }
        },500);
    }







}

//读取cookie
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    };

    return null;
}

//设置cookie
function setCookie(name, value, days) {
    var argc = setCookie.arguments.length;
    var argv = setCookie.arguments;
    var secure = (argc > 5) ? argv[5] : false;
    var expire = new Date();
    if(days==null || days==0) days=1;
    expire.setTime(expire.getTime() + 3600000*24*days);
    document.cookie = name + "=" + escape(value) + ("; path=/") + ((secure == true) ? "; secure" : "") + ";expires="+expire.toGMTString();
}



//弹出对话框
function omnipotent(id, linkurl, title, close_type, w, h) {
    if (!w) w = 700;
    if (!h) h = 500;
    Wind.css('artDialog');
    Wind.use("artDialog","iframeTools",function(){
        art.dialog.open(linkurl, {
            id: id,
            title: title,
            width: w,
            height: h,
            lock: true,
            fixed: true,
            background:"#CCCCCC",
            opacity:0,
            button: [{
                name: '确定',
                callback: function () {
                    if (close_type == 1) {
                        return true;
                    } else {
                        var d = this.iframe.contentWindow;
                        var form = d.document.getElementById('dosubmit');
                        form.click();
                    }
                    return false;
                },
                focus: true
            }]
        });
    });

}


$('.weui-menu-inner').click(function () {
    var $menu = $(this).find('ul'),
        height = $menu.find('li').length * 40 + 15 + 'px',
        opacity = $menu.css('opacity');

    $('.weui-menu-inner ul').css({
        'top': '0',
        'opacity': '0'
    });

    if(opacity == 0) {
        $menu.css({
            'top': '-' + height,
            'opacity': 1
        });
    }else {
        $menu.css({
            'top': 0,
            'opacity': 0
        });
    }
});